//
//  MyStockListController.m
//  Stock
//
//  Created by blacksun on 11-6-16.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "LocalStockListController.h"
#import "AddMyStockListController.h"
#import "StockAppHelper.h"
#import "StockRadar.h"


@implementation LocalStockListController
@synthesize tableView;
@synthesize localStocks;
@synthesize radar;

- (void)dealloc
{
    self.tableView = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reciveDeleteStockNotification:) name:DeleteStockNotification object:nil];
    
}
/*
-(void)reciveDeleteStockNotification:(NSNotification *)notification{
    [self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.3];
}*/

- (void)viewDidUnload{
    [super viewDidUnload];
    self.tableView = nil;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.localStocks count];
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        
        UIColor *clearColor = [UIColor clearColor];
        
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(4.0, 12.0, 70.0, 20.0)];
        nameLabel.backgroundColor = clearColor;
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.tag = kStockNameTag;
        
        UIImageView *differenceImage = [[UIImageView alloc] initWithFrame:CGRectMake(60.0, 2.0, 15.0, 15.0)];
        differenceImage.tag = kStockDifferenceTag;
        
        UILabel *todayPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(80.0, 12.0, 70.0, 20.0)];
        todayPriceLabel.backgroundColor = clearColor;
        todayPriceLabel.tag = kStockTodayPriceTag;
        todayPriceLabel.textAlignment = UITextAlignmentRight;
        UILabel *changePriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(155.0, 12.0, 70.0, 20.0)];
        changePriceLabel.backgroundColor = clearColor;
        changePriceLabel.tag = kStockChangePriceTag;
        changePriceLabel.textAlignment = UITextAlignmentRight;
        UILabel *changeRateLabel = [[UILabel alloc] initWithFrame:CGRectMake(230.0, 12.0, 80.0, 20.0)];
        changeRateLabel.backgroundColor = clearColor;
        changeRateLabel.tag = kStockChangeRateTag;
        changeRateLabel.textAlignment = UITextAlignmentRight;
        
        [cell.contentView addSubview:nameLabel];
        [cell.contentView addSubview:todayPriceLabel];
        [cell.contentView addSubview:changePriceLabel];
        [cell.contentView addSubview:changeRateLabel];
        [cell.contentView addSubview:differenceImage];
        
        [nameLabel release];
        [todayPriceLabel release];
        [changePriceLabel release];
        [changeRateLabel release];
        [differenceImage release];
        
    }
    NSInteger index = [indexPath row];
    NSString *imageName = @"cellbg1.png";
    if (index%2==1) {
        imageName = @"cellbg2.png";
    }
    cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    MyStock *currentStock = [localStocks objectAtIndex:index];
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:kStockNameTag];
    UILabel *todyLabel = (UILabel *)[cell viewWithTag:kStockTodayPriceTag];
    UILabel *priceLabel = (UILabel *)[cell viewWithTag:kStockChangePriceTag];
    UILabel *rateLabel = (UILabel *)[cell viewWithTag:kStockChangeRateTag];
    UIImageView *differenceImage = (UIImageView *)[cell viewWithTag:kStockDifferenceTag];
    int tempAmount = currentStock.crDifference+currentStock.cnDifference+currentStock.prDifference+currentStock.mhDifference;
    int amount = tempAmount>9 ? 9 : tempAmount;
    differenceImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"red_%d.png",amount]];
    
    UIColor *redColor = [UIColor redColor];
    UIColor *greenColor = [UIColor greenColor];
    todyLabel.textColor = [UIColor whiteColor];
    priceLabel.textColor = [currentStock.changedPrice hasPrefix:@"-"]?greenColor:redColor;
    rateLabel.textColor = [currentStock.changedRate hasPrefix:@"-"]?greenColor:redColor;
    nameLabel.text = currentStock.name;
    if (currentStock.closingPrice && currentStock.closingPrice != 0) {
        todyLabel.text = [currentStock.closingPrice stringValue];
        priceLabel.text = currentStock.changedPrice;
        rateLabel.text = currentStock.changedRate;    
    }else{
        todyLabel.text = @"--";
        priceLabel.text = @"--";
        rateLabel.text = @"--";  
    }
    
    return cell;
}



#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyStock *stock = [localStocks objectAtIndex:indexPath.row];
    if ([StockRadar countByStockId:stock.uuid]>=6) {
        [[StockAppHelper shareInstance] showAlertViewWithMessage:@"同一股票最多能添加6个阀值预警"];
        return;
    }
    radar.stockId = stock.uuid;
    [self.navigationController popViewControllerAnimated:YES];
}

@end
